package com.fc.campusapplicationsystem.service;

import com.fc.campusapplicationsystem.mapper.ConsumerMapper;
import com.fc.campusapplicationsystem.model.Consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 贺哲
 * @2020-02-29 15:15
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Autowired
    ConsumerMapper consumerMapper;

    @Override
    public Consumer consumerLogin(String username, String password) {
        return consumerMapper.selectOne(new Consumer().
                setUsername(username).
                setPassword(password));
    }

    @Override
    public List<Consumer> list() {
        return consumerMapper.selectAll();
    }

    @Override
    public Integer add(Consumer consumer) {
        return consumerMapper.insertSelective(consumer);
    }

    @Override
    public Integer delete(Integer id) {
        return consumerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public boolean checkUsername(Consumer consumer) {
        List<Consumer> consumerList = consumerMapper.selectAll();
        for (Consumer c : consumerList) {
            if (c.getUsername().equals(consumer.getUsername())){
                return false;
            }
        }
        return true;
    }

    @Override
    public Integer update(Consumer consumer) {
        return consumerMapper.updateByPrimaryKey(consumer);
    }
}
